//
//  Status.swift
//  swift_demo_weibo
//
//  Created by 杨兵 on 15/5/17.
//  Copyright (c) 2015年 bing. All rights reserved.
//  微博模型

import UIKit
import SDWebImage

private let WE_Home_Timeline_URL = "https://api.weibo.com/2/statuses/home_timeline.json"
private let WE_UNREAD_URL = "https://rm.api.weibo.com/2/remind/unread_count.json"

class Status: NSObject {
    // 创建时间
    var created_at: String? {
        didSet {
            createDateStr = NSDate.formatStringToString(created_at!)
        }
    }
    // 准换后的时间
    var createDateStr: String?
    
    // 微博ID
    var id: Int = 0
    // 微博信息内容
    var text: String?
    // 微博来源
    var source:String?
    // 微博配图,数组中保存字典
    var pic_urls:[[String: String]]? {
        didSet {
            imageUrls = [NSURL]()
            largeUrls = [NSURL]()
            
            for thumbnail in pic_urls! {
                if let url = NSURL(string: thumbnail["thumbnail_pic"]!) {
                    // 有小图获取大图路径获取大图
                    let largeUrl = (url.absoluteString! as NSString).stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                    imageUrls?.append(url)
                    largeUrls?.append(NSURL(string: largeUrl)!)
                }
            }
        }
    }
    
    private var imageUrls:[NSURL]?
    // 大图地址
    private var largeUrls:[NSURL]?
    
    // 计算性数值,用于最终显示图片
    var pictureURLs: [NSURL]? {
        if retweeted_status != nil {
            return retweeted_status!.imageUrls
        } else {
            return imageUrls
        }
    }

    // 保存大图url地址
    var largePicture: [NSURL]? {
        return retweeted_status != nil ? retweeted_status?.largeUrls : largeUrls
    }
    
    
    // 转发数
    var resposts_count: Int = 0
    // 评论数
    var comment_count: Int = 0
    // 表态数
    var attitudes_count: Int = 0
    
    
    // 添加用户对象
    var user:User?
    // 转发微博的图片
    var retweeted_status: Status?
    
    // 属性数组
    static let properties = ["created_at","id","text","source","pic_urls","resposts_count","comment_count","attitudes_count"]
    

    
    init(dict: [String: AnyObject]) {
        super.init()
        
        for key in Status.properties {
            if dict[key] == nil {
                continue
            }
            self.setValue(dict[key], forKeyPath: key)
        }
        
        if let rewdict = dict["retweeted_status"] as? [String: AnyObject]{
            retweeted_status = Status(dict: rewdict)
        }
        
        if let userdict = dict["user"] as? [String: AnyObject] {
            self.user = User(dict: userdict)
        }
    }
    
    
    // 加载数据模型,用户登陆后加载数据
    class func loadStatus(since_Id since_id: Int, max_id: Int, complete:(statuses: [Status]?) -> ()) {
        if userInfoShared == nil {
            return
        }
        
        StatusDAL.DALloadStatus(since_Id: since_id, max_id: max_id) { (statuses) -> () in
            complete(statuses: statuses)
        }
    }
    
    
    
    // 用户未读信息
    class func unreadMsg(complete:(count: Int) -> ()) {
        // 注意用户注销之后不再有access_token
        if userInfoShared == nil {
            return
        }
        
        var params = ["access_token": userInfoShared!.access_token, "uid": userInfoShared!.uid!]
        NetWorkingTools.requestJSON(.GET, URLString: WE_UNREAD_URL, parameters: params) { (JSON) -> () in
            let dict = JSON as! NSDictionary
            if let sta = dict["status"] as? Int {
                complete(count: sta)
            }
        }
        
    }
    
    
    
    // 将字典转换为对象
    class func statusList(array:[[String: AnyObject]])-> [Status]? {
        var arrayM = [Status]()
        for dict in array {
            arrayM.append(Status(dict: dict))
        }
        
        return arrayM
    }
    
    
}
